extends ShapeCast2D


class_name HitBoxComponent
"""
子弹碰撞检测组件
"""


## 碰撞事件
signal event_hit_something(hitbox: HitBoxComponent, body: Node2D, body_rid: RID, hit_normal: Vector2)


## 是否启用
@export var is_enable: bool = true: set = __set_enable
## 是否冻结中
@export var is_frozen: bool = false


# 是否已经初始化
var is_node_frozen: bool = false


# 对命中的对象去重,只触发一次on_hit_something
var __cached_pool: Array[Object] = []


func __set_enable(value: bool) -> void:
    is_enable = value
    enabled = value
    IComponent.set_enable_process(self, value)

func __check_exports() -> Dictionary:
    return {}

func __init_hooks() -> Dictionary:
    return {}

func __prepare() -> void:
    IComponent.prepare(self)

func __reset() -> void:
    IComponent.reset(self)

func _get_configuration_warnings():
    return IComponent.editor_warnings(self)

func _ready():
    IComponent.ready(self)

func _physics_process(delta):
    if is_colliding():
        for i in range(get_collision_count()):
            var body: Object = get_collider(i)
            if __cached_pool.has(body):
                continue
            __cached_pool.append(body)
            event_hit_something.emit(self, body, get_collider_rid(i), get_collision_normal(i))
    else:
        __cached_pool.clear()
